package alvin.hwang.tinygrid;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;


public class DataGenerator {
	
	private static final int ROW_TOTAL = 20;
	private static final int CELL_TOTAL = 15;
	private static final boolean GEN_CHECKBOX = true;
	private static final String DEFAULT_INFO = "-aaaa-";

	private static final int PAGE = 1;
	private static final int TOTAL = ROW_TOTAL;
	
	public static void writeXml() throws IOException {
		System.out.println("--------------------starting---------------------");
		File file = new File("WebRoot/tinygrid/data.xml");
		Writer out = new FileWriter(file);
		out.write(generateXml(PAGE, ROW_TOTAL));
		out.flush();
		out.close();
		System.out.println("--------------------done---------------------");
	}

	public static String generateXml(int page, int rp) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
		buffer.append("<rows>\n");
		buffer.append("<page>").append(page).append("</page>\n");
		buffer.append("<total>").append(TOTAL).append("</total>\n");
		for (int row = 0; row < rp; row++) {
			buffer.append("<row>\n");
			for (int cell = 0; cell < CELL_TOTAL; cell++) {
				if (cell == 0 && GEN_CHECKBOX) {
					buffer.append("<cell><![CDATA[ <input type='checkbox' name='ch' value='").append(row).append(cell).append("' />]]></cell>\n");
				}
				buffer.append("<cell><![CDATA[").append(DEFAULT_INFO).append(row).append(cell).append("]]></cell>\n");
			}
			buffer.append("</row>\n");
		}
		buffer.append("</rows>");
		return buffer.toString();
	}
	
	public static void main(String[] args) throws IOException {
		DataGenerator.writeXml();
	}
}
